package main

import (
	"fmt"
	"net/http"
	"os"
)

/*
4) 当访问 localhost/healthz 时, 应返回 200
 */
func healthz(response http.ResponseWriter, request *http.Request) {
	fmt.Println("healthz path log")
	// 3) Server 端记录访问日志包括客户端 IP, HTTP 返回码, 输出到 server 端的标准输出
	responseStatus := 200
	requestIp := request.Host
	response.WriteHeader(responseStatus)
	fmt.Println("requestIp:", requestIp, " http response status:", responseStatus)

	requestUri := request.RequestURI
	fmt.Println(" requestUri:", requestUri)
	header := request.Header
	for headKey := range header {
		headValue := header.Get(headKey)
		fmt.Println("headKey", headKey, " headValue:", headValue)
		// 1) 接收客户端 request, 并将 request 中带的 header 写入 response header
		response.Header().Add(headKey, headValue)
	}

	// 2) 读取当前系统的环境变量中的 VERSION 配置, 并写入 response header
	osVersion := os.Getenv("VERSION")
	fmt.Println("os VERSION:", osVersion)
	response.Header().Add("VERSION", osVersion)


}


func main() {
	fmt.Println("云原生 第二周作业")

	// 4) 当访问 localhost/healthz 时, 应返回 200
	http.HandleFunc("/healthz", healthz)
	http.ListenAndServe("127.0.0.1:9001", nil)

}


